/*
* @Author: songwuhong
* @Date:   2017-04-10 14:57:41
 * @Last Modified by: songwh
 * @Last Modified time: 2017-12-07 10:49:04
*/

import { modal } from 'vue-strap'
import { showMsg, saveSectionItem, toogleSectionListFresh } from '../../../vuex/action'

export default {
  name: 'product-new',
  components: {
    'vue-modal': modal
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xToogleIndexAdListFresh: toogleSectionListFresh,
      xSaveSectionItem: saveSectionItem
    }
  },
  props: ['showNew', 'it', 'title'],
  data() {
    return {
    }
  },
  computed: {
  },
  ready() {
  },
  methods: {
    submit() {
      if (!this.it.name){
        return this.xShowMsg({
          type: 'danger',
          content: '* 标记的为必填项'
        })
      }
      var params = {
        name: this.it.name,
        data1: this.it.data1,
        data2: this.it.data2,
        type: this.it.type
      }
      if (this.it.sectionId){
        params.sectionId = this.it.sectionId
      }
      var formData = new FormData()
      for (let name in params) {
        if (name !== 'picFile') {
          formData.append(name, params[name])
        }
      }
      formData.append('picFile', this.$els.file.files[0])
      this.xSaveSectionItem(formData)
        .then((data) => {
          this.showNew= false
          this.xShowMsg({
            type: 'danger',
            content: '操作成功！'
          })
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    showNew() {
      if (!this.showNew) {
        this.xToogleIndexAdListFresh()
      }
    }
  },
}
